/* EasyWay Game Engine
 * Copyright (C) 2006 Daniele Paggi.
 *  
 * Written by: 2006 Daniele Paggi<dshnt@hotmail.com>
 *   
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 * 
 * You should have received a copy of the GNU Library General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
package org.easyway.tiles;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;

import org.easyway.utils.Utility;

/**
 * 
 * @author Daniele Paggi
 * 
 */
public class TileSet implements Serializable {

	/**
	 * generated version id
	 */
	private static final long serialVersionUID = 790915426908315894L;

	/**
	 * the list of tiles
	 */
	protected ArrayList<Tile> tileList;
	
	/**
	 * the file name
	 */
	String pathFilename;

	public TileSet(int numberTiles,String name) {
		tileList = new ArrayList<Tile>(numberTiles);
		pathFilename = name;
	}

	public TileSet(String name) {
		tileList = new ArrayList<Tile>(255);
		pathFilename = name;
	}

	public TileSet(ArrayList<Tile> tiles, String name) {
		tileList = tiles;
		pathFilename = name;
	}

	// ------------------

	/**
	 * return the tileList
	 * 
	 * @return the tileList
	 */
	public ArrayList<Tile> getTileList() {
		return tileList;
	}

	/**
	 * changes the tileList
	 * 
	 * @param tileList
	 *            the new tileList to set
	 */
	public void setTileList(ArrayList<Tile> tileList) {
		this.tileList = tileList;
	}

	/**
	 * returns the tile number 'index'
	 * 
	 * @param index
	 *            the index of the tile to get
	 * @return the tile or null if the index is outofbounds
	 */
	public Tile get(int index) {
		if (index < 0 || index >= tileList.size())
			return null;
		return tileList.get(index);
	}

	public void add(Tile tile) {
		tileList.add(tile);
	}

	public void add(Tile tile, int index) {
		tileList.add(index, tile);
	}

	private void readObject(java.io.ObjectInputStream s)
			throws java.io.IOException, ClassNotFoundException {
		s.defaultReadObject();
		for (int i = 0; i < tileList.size(); ++i) {
			tileList.get(i).id = i;
		}
	}
	
	
	public String getName() {
		return pathFilename;
	}
	
	public void setName( String pathFilename ) {
		this.pathFilename = pathFilename;
	}

	public static TileSet readFromFile(String file) {

		ObjectInputStream in = Utility.getLocalFile(file);
		if (in == null)
			return null;

		try {			
			// read the version (at this time it's only 1)
			/* int version = */in.readInt();

			int size = in.readInt();
			TileSet tileSet = new TileSet(size,file);
			
			Tile tile;
			for (int i = 0; i < size; ++i) {
				System.out.println("loading: "+i);
				tile = Tile.readFromFile(in);
				tile.id = i;
				tileSet.add(tile);
			}

			in.close();
			return tileSet;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public void writeOnFile(String filename) {
		pathFilename = filename;
		if (tileList == null || tileList.size() == 0) {
			Utility.error("tileSet is empty; What I should Write??",
					new Exception("tileSet is Empty"));
			return;
		}

		try {
			FileOutputStream fout = new FileOutputStream(filename);
			ObjectOutputStream out = new ObjectOutputStream(fout);

			// the version-id
			out.writeInt(1);

			out.writeInt(tileList.size());
			for (int i = 0; i < tileList.size(); ++i) {
				tileList.get(i).writeOnFile(out);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
